package com.dpiinc.TechPage.room;

import android.content.Context;

import com.dpiinc.TechPage.oss.UploadTask;
import com.dpiinc.TechPage.room.bean.LabelEntity;
import com.dpiinc.TechPage.room.bean.NoteEntity;
import com.dpiinc.TechPage.room.bean.PenEntity;
import com.dpiinc.TechPage.room.bean.RecognizeEntity;
import com.dpiinc.TechPage.room.bean.UserEntity;
import com.dpiinc.TechPage.room.dao.UploadTaskDao;
import com.dpiinc.TechPage.room.dao.LabelDao;
import com.dpiinc.TechPage.room.dao.NoteDao;
import com.dpiinc.TechPage.room.dao.PenDao;
import com.dpiinc.TechPage.room.dao.RecognizeDao;
import com.dpiinc.TechPage.room.dao.UserDao;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

/**
 * Created by hu489_000 on 2018-07-01.
 */


@Database(entities = {UserEntity.class, NoteEntity.class, LabelEntity.class, PenEntity.class, RecognizeEntity.class, UploadTask.class}, version = 2, exportSchema = false)
public abstract class RoomAiWriterDatabase extends RoomDatabase {

    private static final String TAG = "RoomAiWriterDatabase";
    private static RoomAiWriterDatabase INSTANCE;
    private static final Object sLock = new Object();

    public abstract UserDao userDao();

    public abstract PenDao penDao();

    /**
     * 笔记Dao
     *
     * @return NoteDao
     */
    public abstract NoteDao noteDao();


    public abstract RecognizeDao recognizeDao();

    /**
     * 标签Dao
     *
     * @return LabelDao
     */
    public abstract LabelDao labelDao();

    /**
     * 缓存文件Dao
     *
     * @return UploadTaskDao
     */
    public abstract UploadTaskDao uploadTaskDao();

    public static RoomAiWriterDatabase getInstance(Context context) {
        synchronized (sLock) {
            if (INSTANCE == null) {
                INSTANCE =
                        Room.databaseBuilder(context.getApplicationContext(), RoomAiWriterDatabase.class, "RoomAiWriterDatabase.db")
                                .allowMainThreadQueries()
                                .fallbackToDestructiveMigration()
//                                .addMigrations(MIGRATION_1_2)
                                .build();
            }
            return INSTANCE;
        }

    }

    static final Migration MIGRATION_1_2 = new Migration(1, 2) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE UserEntity "
                    + " ADD COLUMN openid TEXT");
        }
    };
}
